package com.example.campus.service.impl;

import com.example.campus.exception.PermissionsExceededException;
import com.example.campus.exception.QueryFailedException;
import com.example.campus.mapper.CommentMapper;
import com.example.campus.mapper.LostAndFoundInfoMapper;
import com.example.campus.mapper.UserMapper;
import com.example.campus.pojo.dto.CommentPageQueryDTO;
import com.example.campus.pojo.dto.CommentPublishDTO;
import com.example.campus.pojo.entity.Comment;
import com.example.campus.pojo.entity.User;
import com.example.campus.pojo.vo.CommentVO;
import com.example.campus.result.PageResult;
import com.example.campus.service.CommentService;
import com.example.campus.utils.constant.MessageConstant;
import com.example.campus.utils.context.BaseContext;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private LostAndFoundInfoMapper lostAndFoundInfoMapper;

    @Override
    public PageResult getCommentList(CommentPageQueryDTO commentPageQueryDTO) {
        PageHelper.startPage(commentPageQueryDTO.getPage(), commentPageQueryDTO.getPageSize());
        Page<CommentVO> page = commentMapper.getCommentList(commentPageQueryDTO);
        long total = page.getTotal();
        if (total < 1) {
            throw new QueryFailedException(MessageConstant.QUERY_NULL);
        }
        List<CommentVO> records = page.getResult();
        return new PageResult(total, records);
    }

    @Override
    public void deleteById(Long id) {
        commentMapper.deleteById(id);
    }

    @Override
    public List<CommentVO> getCommentByInfoId(Long infoId) {
        List<Comment> comments = commentMapper.getByInfoId(infoId);
        List<CommentVO> commentVOS = new ArrayList<>();
        for (Comment comment : comments) {
            CommentVO commentVO = new CommentVO();
            BeanUtils.copyProperties(comment, commentVO);
            User user = userMapper.getById(commentVO.getUserId());
            commentVO.setUsername(user.getUsername());
            commentVO.setAvatarPath(user.getAvatarPath());
            commentVOS.add(commentVO);
        }
        return commentVOS;
    }

    @Override
    public void publishComment(CommentPublishDTO commentPublishDTO) {
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentPublishDTO, comment);
        comment.setUserId(BaseContext.getCurrentId());
        comment.setCreateTime(LocalDateTime.now());
        commentMapper.insert(comment);
    }

    @Override
    public void deleteComment(Long id) {
        Comment comment = commentMapper.getById(id);
        Long currentId = BaseContext.getCurrentId();
        Long userId = comment.getUserId();
        Long poster = lostAndFoundInfoMapper.getById(comment.getInfoId()).getUserId();
        if (!userId.equals(currentId) && !poster.equals(currentId)) {
            throw new PermissionsExceededException(MessageConstant.PERMISSION_EXCEEDED);
        }
        commentMapper.deleteById(id);
    }

    @Override
    public Long getCommentTotal() {
        return commentMapper.getCommentTotal();
    }
}
